package com.example.learndemo.JVM;

import java.io.FileInputStream;
import java.lang.reflect.Method;

public class MyClassLoaderTest {
    static{
        System.out.println(MyClassLoaderTest.class.getName()+"==>开始加载");
    }
	static class MyClassLoader extends ClassLoader {
		private String classPath;

		public MyClassLoader(String classPath) {
			this.classPath = classPath;
		}

        private byte[] loadByte(String name) throws Exception {
            name = name.replaceAll("\\.", "/");
            FileInputStream fis = new FileInputStream(classPath + "/" + name
                    + ".class");
            int len = fis.available();
            byte[] data = new byte[len];
            fis.read(data);
            fis.close();
            return data;
        }

        protected Class<?> findClass(String name) throws ClassNotFoundException {
            try {
                byte[] data = loadByte(name);
//                data[4]=1;
                //defineClass将一个字节数组转为Class对象，这个字节数组是class文件读取后最终的字节数组。
                return defineClass(name, data, 0, data.length);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ClassNotFoundException();
            }
        }

	}

	public static void main(String[] args) throws Exception {
		MyClassLoader myClassLoader = new MyClassLoader(
				"C:\\Users\\shijiangbo\\Desktop\\log");
		Class<?> clazz = myClassLoader.loadClass("com.example.learndemo.User");
		Object o = clazz.newInstance();
		Method setFileId = clazz.getDeclaredMethod("sout");
        setFileId.invoke(o);
        System.out.println(clazz.getClassLoader().getClass().getName());
	}
}
